package com.success.application.base;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.success.application.myapplication.R;

import butterknife.Bind;

/**
 * Created by ChrisWong on 2017/1/18.
 */

public abstract class BaseViewPagerFragment extends BaseTitleFragment {

    @Bind(R.id.tab_nav)
    protected TabLayout mTabNav;

    @Bind(R.id.base_viewPager)
    protected ViewPager mBaseViewPager;

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_base_view_pager;
    }

    protected void initBundle(Bundle bundle){
        super.initBundle(bundle);
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        BaseViewPagerAdapter adapter = new BaseViewPagerAdapter(getChildFragmentManager(), getPagers());
        ViewPager mBaseViewPager =(ViewPager)root.findViewById(R.id.base_viewPager);
        mBaseViewPager.setAdapter(adapter);
        TabLayout mTabNav = (TabLayout)root.findViewById(R.id.tab_nav);
        mTabNav.setupWithViewPager(mBaseViewPager);
        mBaseViewPager.setCurrentItem(0, true);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    protected abstract PagerInfo[] getPagers();

    public static class PagerInfo {
        private String title;
        private Class<?> clx;
        private Bundle args;

        public PagerInfo(String title, Class<?> clx, Bundle args) {
            this.title = title;
            this.clx = clx;
            this.args = args;
        }
    }

    public class BaseViewPagerAdapter extends FragmentPagerAdapter {
        private PagerInfo[] mInfoList;
        private Fragment mCurFragment;

        public BaseViewPagerAdapter(FragmentManager fm, PagerInfo[] infoList) {
            super(fm);
            mInfoList = infoList;
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position, Object object) {
            super.setPrimaryItem(container, position, object);
            if (object instanceof Fragment) {
                Fragment fragment = (Fragment) object;
                mCurFragment = fragment;
            }
        }

        public Fragment getCurFragment() {
            return mCurFragment;
        }

        @Override
        public Fragment getItem(int position) {
            PagerInfo info = mInfoList[position];
            return instantiate(getContext(), info.clx.getName(), info.args);
        }

        @Override
        public int getCount() {
            return mInfoList.length;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mInfoList[position].title;
        }

        @Override
        public int getItemPosition(Object object) {
            return PagerAdapter.POSITION_NONE;
        }
    }

    public void onTabReselect() {
        BaseViewPagerAdapter adapter = (BaseViewPagerAdapter)mBaseViewPager.getAdapter();
        Fragment fragment = adapter.getCurFragment();
        if(fragment instanceof BaseRecyclerViewFragment){
            ((BaseRecyclerViewFragment)fragment).onTabReselect();
        }
        if(fragment instanceof BaseViewPagerFragment){
            ((BaseViewPagerFragment)fragment).onTabReselect();
        }
    }

    /**
     * 修改TabLayout标签显示值，用于子列表更新后调用
     * @param index tab序号
     * @param title 新标签名
     */
    public void changeTitle(int index, String title) {
        TabLayout.Tab tab = mTabNav.getTabAt(index);
        tab.setText(title);
    }

}
